<?php
declare (strict_types = 1);
namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Cache;

//Token管理类
class Sms{

    var $sms_sign="【仙居平安驾校】";

    public function __construct(){

    }


    public function index(){

    }

    //普通短信发送
    public function sign_up_sms($customer){
        if(empty($customer)) return ['code'=>0,'msg'=>'Mobile参数缺失.'];

        $text=" 亲爱的".$customer['name']."，谢谢您的信任，祝您学车愉快，生活幸福！";
        return $this->yunpian_sms($text,$customer['mobile'],$customer['name']);
    }

    //客户端取得验证码
    public function get_verify_code(){
        $all= Request::param();
        if(empty($all['mobile'])) return json(['code'=>0,'msg'=>'Mobile参数缺失.']);
        $mobile =$all['mobile'];

        $verify_code = rand(1000,9999);
        Cache::set($mobile,$verify_code);
        $text=" 你的验证码是".$verify_code.",谢谢。";
        $ss = $this->yunpian_sms($text,$mobile);

        return json(['code'=>1,'msg'=>'验证码已发送至手机号:'.$all['mobile'].',请注意查收.','verify_code'=>Cache::get($mobile),'msg'=>$ss]);
    }

    public function yunpian_sms($text='',$mobile='',$cname=''){
        $apikey = "db619adc2ac02f2b776a122338fb4172"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
        //$mobile = "xxxxxxxxxxx"; //请用自己的手机号代替
        $text=$this->sms_sign.$text;

        $ch = curl_init();

        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 取得用户信息
        //$json_data = $this->get_user($ch,$apikey);
        //$array = json_decode($json_data,true);
        //print_r($array);


        if(!$cname){
        // 发送短信
            $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
            $json_data =$this->send($ch,$data);
            
            //$array = json_decode($json_data,true);
        }else{
            // 发送模板短信
            // 需要对value进行编码
            $data = array(
                'tpl_id' => '5786282', 
                'tpl_value' =>  ('#name#').'='.urlencode($cname), 
                'apikey' => $apikey, 
                'mobile' => $mobile);

            print_r ($data);
            $json_data = $this->tpl_send($ch,$data);
            $array = json_decode($json_data,true);
            echo '<pre>';
            print_r($array);
        }

        // 发送语音验证码
        // $data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
        // $json_data =voice_send($ch,$data);
        // $array = json_decode($json_data,true);
        // echo '<pre>';
        // print_r($array);

        curl_close($ch);
        return $json_data;

    }

/************************************************************************************/
//获得账户
function get_user($ch,$apikey){
    curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
    $result = curl_exec($ch);
    $error = curl_error($ch);
    return $this->checkErr($result,$error);
    return $result;
}
function send($ch,$data){
    curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $error = curl_error($ch);
    $this->checkErr($result,$error);
    return $result;
}
function tpl_send($ch,$data){
    curl_setopt($ch, CURLOPT_URL,
        'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $error = curl_error($ch);
    $this->checkErr($result,$error);
    return $result;
}
function voice_send($ch,$data){
    curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $error = curl_error($ch);
    $this->checkErr($result,$error);
    return $result;
}
function checkErr($result,$error) {
    if($result === false)
    {
        echo 'Curl error: ' . $error;
    }
    else
    {
        //echo '操作完成没有任何错误';
    }
}


}
